package com.uzai.console.service.wechatwork.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.dto.device.DeviceInfoBaseCache;
import com.uzai.common.dto.device.DeviceInfoWxWorkCacheDto;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.user.usertype.UserTypeQuery;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupIdDto;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupInfoDto;
import com.uzai.console.dto.wechatwork.openapiinfo.WxworkOpenapiInfoDto;
import com.uzai.console.dto.wechatwork.openapiinfo.WxworkOpenapiInfoIdDto;
import com.uzai.console.dto.wechatwork.openapiinfo.WxworkOpenapiInfoQuery;
import com.uzai.console.entity.UserType;
import com.uzai.console.entity.UserTypeGroup;
import com.uzai.console.entity.WxworkOpenapiInfo;
import com.uzai.console.mapper.WxworkOpenapiInfoMapper;
import com.uzai.console.service.wechatwork.WxworkOpenapiInfoService;
import com.uzai.console.vo.wechatwork.openapiinfo.WxworkOpenapiInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业微信授权渠道
 * @author liuqi
 * @date 2021年08月19日 16:50
 */
@Service
public class WxworkOpenapiInfoServiceImpl implements WxworkOpenapiInfoService {

    Logger logger = LoggerFactory.getLogger(WxworkOpenapiInfoServiceImpl.class);

    @Autowired
    private WxworkOpenapiInfoMapper wxworkOpenapiInfoMapper;

    /**
     * 查询列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxworkOpenapiInfoQuery
     * @return null
     */
    public Page<WxworkOpenapiInfoVo> findWxworkOpenapiInfoPage(WxworkOpenapiInfoQuery wxworkOpenapiInfoQuery){

        //新建返回到页面的LIST对象
        List<WxworkOpenapiInfoVo> wxworkOpenapiInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<WxworkOpenapiInfo> page = new Page<>();
        wxworkOpenapiInfoQuery.convert(page);
        ArrayList<WxworkOpenapiInfo> wxworkOpenapiInfoList =  wxworkOpenapiInfoMapper.findByList(page, wxworkOpenapiInfoQuery);
        if(wxworkOpenapiInfoList != null && wxworkOpenapiInfoList.size() > 0){
            for (WxworkOpenapiInfo wxworkOpenapiInfo : wxworkOpenapiInfoList){
                WxworkOpenapiInfoVo wxworkOpenapiInfoPageVo = new WxworkOpenapiInfoVo();
                BeanUtils.copyProperties(wxworkOpenapiInfo, wxworkOpenapiInfoPageVo);
                wxworkOpenapiInfoVoList.add(wxworkOpenapiInfoPageVo);
            }
        }
        //返回到页面分页对象
        Page<WxworkOpenapiInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(wxworkOpenapiInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxworkOpenapiInfoIdDto
     * @return WxworkOpenapiInfoVo
     */
    public WxworkOpenapiInfoVo findWxworkOpenapiInfo(WxworkOpenapiInfoIdDto wxworkOpenapiInfoIdDto){
        if(wxworkOpenapiInfoIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }
        WxworkOpenapiInfo wxworkOpenapiInfo = wxworkOpenapiInfoMapper.selectById(wxworkOpenapiInfoIdDto.getId(), wxworkOpenapiInfoIdDto.getMerId());

        if(wxworkOpenapiInfo == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该企业微信授权渠道不存在");
        }
        WxworkOpenapiInfoVo wxworkOpenapiInfoVo = new WxworkOpenapiInfoVo();

        BeanUtils.copyProperties(wxworkOpenapiInfo, wxworkOpenapiInfoVo);

        return wxworkOpenapiInfoVo;
    }

    /**
     * 添加
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxworkOpenapiInfoDto
     * @return null
     */
    public void addWxworkOpenapiInfo(WxworkOpenapiInfoDto wxworkOpenapiInfoDto){
        if(StringUtils.isBlank(wxworkOpenapiInfoDto.getCorpId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入企业id");
        }

        if(StringUtils.isBlank(wxworkOpenapiInfoDto.getCorpName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入企业名称");
        }

        if(StringUtils.isBlank(wxworkOpenapiInfoDto.getSecret())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入Secret");
        }

        //验证是否重名
        WxworkOpenapiInfo userTypeGroup_corpId = wxworkOpenapiInfoMapper.selectByCorpId(wxworkOpenapiInfoDto.getCorpId());
        if(userTypeGroup_corpId != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该企业id已经存在");
        }

        Integer now = DateUtil.getNowTime();
        WxworkOpenapiInfo wxworkOpenapiInfo = new WxworkOpenapiInfo();
        wxworkOpenapiInfo.setId(IdWorker.getId());
        wxworkOpenapiInfo.setMerId(wxworkOpenapiInfoDto.getMerId());
        wxworkOpenapiInfo.setCorpId(wxworkOpenapiInfoDto.getCorpId());
        wxworkOpenapiInfo.setCorpName(wxworkOpenapiInfoDto.getCorpName());
        wxworkOpenapiInfo.setSecret(wxworkOpenapiInfoDto.getSecret());
        wxworkOpenapiInfo.setRemark(wxworkOpenapiInfoDto.getRemark());
        wxworkOpenapiInfo.setCreateTime(now);
        wxworkOpenapiInfo.setUpdateTime(now);
        wxworkOpenapiInfoMapper.insert(wxworkOpenapiInfo);
    }

    /**
     * 修改信息
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxworkOpenapiInfoDto
     */
    public void updateWxworkOpenapiInfo(WxworkOpenapiInfoDto wxworkOpenapiInfoDto){

        if(wxworkOpenapiInfoDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        if(StringUtils.isBlank(wxworkOpenapiInfoDto.getCorpId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入企业id");
        }

        if(StringUtils.isBlank(wxworkOpenapiInfoDto.getCorpName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入企业名称");
        }

        if(StringUtils.isBlank(wxworkOpenapiInfoDto.getSecret())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入Secret");
        }

        WxworkOpenapiInfo wxworkOpenapiInfo = wxworkOpenapiInfoMapper.selectById(wxworkOpenapiInfoDto.getId(), wxworkOpenapiInfoDto.getMerId());
        if(wxworkOpenapiInfo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该企业微信不存在");
        }

        //验证新名字是否已经存在
        if(!wxworkOpenapiInfoDto.getCorpId().equals(wxworkOpenapiInfo.getCorpId())){ //表示修改了企业id
            //验证新企业id是否存在
            WxworkOpenapiInfo wxworkOpenapiInfo_corpId = wxworkOpenapiInfoMapper.selectByCorpId(wxworkOpenapiInfoDto.getCorpId());
            if(wxworkOpenapiInfo_corpId != null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该企业id已经存在");
            }
        }

        Integer now = DateUtil.getNowTime();
        wxworkOpenapiInfo.setCorpId(wxworkOpenapiInfoDto.getCorpId());
        wxworkOpenapiInfo.setCorpName(wxworkOpenapiInfoDto.getCorpName());
        wxworkOpenapiInfo.setSecret(wxworkOpenapiInfoDto.getSecret());
        wxworkOpenapiInfo.setRemark(wxworkOpenapiInfoDto.getRemark());
        wxworkOpenapiInfo.setUpdateTime(now);
        wxworkOpenapiInfoMapper.updateById(wxworkOpenapiInfo);
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxworkOpenapiInfoIdDto
     * @return null
     */
    public void deleteWxworkOpenapiInfo(WxworkOpenapiInfoIdDto wxworkOpenapiInfoIdDto){
        if(wxworkOpenapiInfoIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }
        wxworkOpenapiInfoMapper.deleteById(wxworkOpenapiInfoIdDto.getId(), wxworkOpenapiInfoIdDto.getMerId());
    }

    /**
     * 查询列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxworkOpenapiInfoQuery
     * @return null
     */
    public List<WxworkOpenapiInfoVo> findWxworkOpenapiInfoList(WxworkOpenapiInfoQuery wxworkOpenapiInfoQuery){

        //新建返回到页面的LIST对象
        List<WxworkOpenapiInfoVo> wxworkOpenapiInfoVoList = new ArrayList<>();
        ArrayList<WxworkOpenapiInfo> wxworkOpenapiInfoList =  wxworkOpenapiInfoMapper.findByList(wxworkOpenapiInfoQuery);
        if(wxworkOpenapiInfoList != null && wxworkOpenapiInfoList.size() > 0){
            for (WxworkOpenapiInfo wxworkOpenapiInfo : wxworkOpenapiInfoList){
                WxworkOpenapiInfoVo wxworkOpenapiInfoPageVo = new WxworkOpenapiInfoVo();
                BeanUtils.copyProperties(wxworkOpenapiInfo, wxworkOpenapiInfoPageVo);
                wxworkOpenapiInfoVoList.add(wxworkOpenapiInfoPageVo);
            }
        }
        return wxworkOpenapiInfoVoList;
    }
}
